﻿@model SuggestModel
@using NewLife;
@using NewLife.Serialization;
@using NewLife.Web;
@using XCode;
@using System.Linq;
@using NewLife.Cube.ViewModels;
@{
    var id = Model.Id;
    var page = ViewBag.Page as Pager;

    var val = Model.Value;
    if (val.IsNullOrEmpty()) val = page[id];

    var set = NewLife.Cube.CubeSetting.Current;
    var res = set.ResourceUrl;
    if (String.IsNullOrEmpty(res)) res = "/Content";
    res = res.TrimEnd('/');
}

<div class="form-group" style="position:relative">
    <div class="col-lg-12">
        <div class="input-group">
            <input type="hidden" name="@id" id="@id" value="@val" />
            <input type="text" class="form-control" id="@(id)_select" placeholder="@Model.PlaceHolder" value="@Model.ShowValue">
            <div class="input-group-btn">
                <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
            </div>
        </div>
    </div>
</div>

<script src="@res/bootstrap-suggest.js"></script>
<script type="text/javascript">
    $(function () {
        $("#@(id)_select").bsSuggest({
            url: "@Model.Url",
            getDataMethod:"url",
            effectiveFieldsAlias: @Html.Raw(Model.Fields.ToJson()),
            ignorecase: true,
            showHeader: true,
            delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
            hideOnSelect: true,
            idField: "@Model.IdField",
            showBtn: false,
            keyField: "@Model.NameField",
            clearable: true,
            searchingTip: '搜索中...',
            allowNoKeyword: true,
            delay: 500,
            emptyTip: '无数据',
            containerValueId: '@id',
            fnProcessData: function (result) {
                dt = {};
                dt.value = result.data;
                return dt;
            }
        }).on('onSetSelectValue', function (e, keyword, data) {
            $('#@id').val(keyword.id);
        }).on('onUnsetSelectValue', function () {
            $('#@id').val('');
        });
    });
</script>